package com.controller;

import com.pojo.Page;
import com.pojo.Worker;
import com.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/worker")
public class WorkerController {
    @Autowired
    private WorkerService workerService;

    @RequestMapping("/allWorker")
    public String list(Model model, HttpServletRequest request, HttpSession session) {

        int pages=0;            //待显示页面
        int count=0;            //总条数
        int totalpages=0;        //总页数
        int limit=5;            //每页显示记录条数

        count = workerService.countWorkers();

        //由记录总数除以每页记录数得出总页数
        totalpages = (int)Math.ceil(count/(limit*1.0));
        //获取跳页时传进来的当前页面参数
        String strPage = request.getParameter("pages");
        String paixu_query_type = request.getParameter("paixu_query_type");
        String up = request.getParameter("up");
        String down = request.getParameter("down");
        //判断当前页面参数的合法性并处理非法页号（为空则显示第一页，小于0则显示第一页，大于总页数则显示最后一页）
        System.out.println("up"+request.getParameter("up"));
        System.out.println("down"+request.getParameter("down"));
        if (strPage == null) {
            pages = 1;
        } else {
            try{
                pages = Integer.parseInt(strPage);
            }catch(Exception e){
                pages = 1;
            }

            if (pages < 1){
                pages = 1;
            }

            if (pages > totalpages){
                pages = totalpages;
            }
        }
        Page page = new Page();
        page.setPage(String.valueOf((pages - 1) * limit));
        page.setLimit(String.valueOf(limit));

        //默认是按照id升序
        List<Worker> list = workerService.queryWorkerByIdUp(page);
        if(paixu_query_type != null) {
            if (paixu_query_type.equals("paixuSelectById")) {
                if(up != null) {
                    if (up.equals("up")) {
                        System.out.println("按照id升序");
                        list = workerService.queryWorkerByIdUp(page);
                    }
                }
                if(down != null){
                    if(down.equals("down")){
                        //按照id降序
                        System.out.println("按照id降序");
                        list = workerService.queryWorkerByIdDown(page);
                    }
                }

            } else if (paixu_query_type.equals("paixuSelectByName")) {
                if(up != null) {
                    if (up.equals("up")) {
                        System.out.println("按照姓名升序");
                        list = workerService.queryWorkerByNameUp(page);
                    }
                }
                if(down != null){
                    if(down.equals("down")){
                        //按照姓名降序
                        System.out.println("按照姓名降序");
                        list = workerService.queryWorkerByNameDown(page);
                    }
                }

            } else if (paixu_query_type.equals("paixuSelectByWorkage")) {
                //按照工龄升序
                if(up != null) {
                    if (up.equals("up")) {
                        System.out.println("按照工龄升序");
                        list = workerService.queryWorkerByWorkageUp(page);
                    }
                }
                if(down != null){
                    if(down.equals("down")){
                        //按照工龄降序
                        System.out.println("按照工龄降序");
                        list = workerService.queryWorkerByWorkageDown(page);
                    }
                }
            }
        }





        System.out.println(list);
        System.out.println("countWorkers"+count);
        model.addAttribute("up",up);
        model.addAttribute("paixu_query_type",paixu_query_type);
        model.addAttribute("down",down);
        model.addAttribute("list", list);
        model.addAttribute("pages", pages);
        model.addAttribute("totalpages", totalpages);

        return "allWorker";
    }




    @RequestMapping("toAddWorker")
    public String toAddWorker() {
        return "addWorker";
    }

    @RequestMapping("/addWorker")
    public String addWorker(Worker w,BindingResult result) {
        workerService.addWorker(w);
        return "redirect:/worker/allWorker";
    }

    @RequestMapping("/del/{id}")
    public String deleteWorker(@PathVariable("id") Long id) {
        workerService.deleteWorkerById(id);
        return "redirect:/worker/allWorker";
    }

    @RequestMapping("toUpdateWorker")
    public String toUpdateWorker(Model model, Long id) {
        model.addAttribute("worker", workerService.queryById(id));
        return "updateWorker";
    }


    @RequestMapping("/updateWorker")
    public String updateWorker(Model model, Worker w) {
        workerService.updateWorker(w);
        w = workerService.queryById(w.getId());
        model.addAttribute("worker", w);
        return "redirect:/worker/allWorker";
    }

    @RequestMapping("/queryResult")
    public String queryResult(Model model,@RequestParam("text") String text,@RequestParam("query_type") String query_type){
        Worker worker = new Worker();
        //如果是按id查询
        if(query_type.equals("selectById"))
            worker = workerService.queryById(Integer.valueOf(text).intValue());
        //如果是按姓名查询
        if(query_type.equals("selectByName"))
           worker = workerService.queryByName(text);


        if(worker == null)
            model.addAttribute("error", "查无此人");

        model.addAttribute("worker", worker);
        return "queryResult";
    }


    @RequestMapping("/queryResult2")
    public String queryResult2(Model model,@RequestParam("text") String text,@RequestParam("query_type") String query_type){
        Worker worker = new Worker();
        //如果是按id查询
        if(query_type.equals("selectById"))
            worker = workerService.queryById(Integer.valueOf(text).intValue());
        //如果是按姓名查询
        if(query_type.equals("selectByName"))
            worker = workerService.queryByName(text);


        if(worker == null)
            model.addAttribute("error", "查无此人");

        model.addAttribute("worker", worker);
        return "queryResult2";
    }



    @RequestMapping("/allYonghu")
    public String list2(Model model, HttpServletRequest request, HttpSession session) {

        int pages=0;            //待显示页面
        int count=0;            //总条数
        int totalpages=0;        //总页数
        int limit=5;            //每页显示记录条数

        count = workerService.countWorkers();

        //由记录总数除以每页记录数得出总页数
        totalpages = (int)Math.ceil(count/(limit*1.0));
        //获取跳页时传进来的当前页面参数
        String strPage = request.getParameter("pages");
        String paixu_query_type = request.getParameter("paixu_query_type");
        String up = request.getParameter("up");
        String down = request.getParameter("down");
        //判断当前页面参数的合法性并处理非法页号（为空则显示第一页，小于0则显示第一页，大于总页数则显示最后一页）
        System.out.println("up"+request.getParameter("up"));
        System.out.println("down"+request.getParameter("down"));
        if (strPage == null) {
            pages = 1;
        } else {
            try{
                pages = Integer.parseInt(strPage);
            }catch(Exception e){
                pages = 1;
            }

            if (pages < 1){
                pages = 1;
            }

            if (pages > totalpages){
                pages = totalpages;
            }
        }
        Page page = new Page();
        page.setPage(String.valueOf((pages - 1) * limit));
        page.setLimit(String.valueOf(limit));

        //默认是按照id升序
        List<Worker> list = workerService.queryWorkerByIdUp(page);
        if(paixu_query_type != null) {
            if (paixu_query_type.equals("paixuSelectById")) {
                if(up != null) {
                    if (up.equals("up")) {
                        System.out.println("按照id升序");
                        list = workerService.queryWorkerByIdUp(page);
                    }
                }
                if(down != null){
                    if(down.equals("down")){
                        //按照id降序
                        System.out.println("按照id降序");
                        list = workerService.queryWorkerByIdDown(page);
                    }
                }

            } else if (paixu_query_type.equals("paixuSelectByName")) {
                if(up != null) {
                    if (up.equals("up")) {
                        System.out.println("按照姓名升序");
                        list = workerService.queryWorkerByNameUp(page);
                    }
                }
                if(down != null){
                    if(down.equals("down")){
                        //按照姓名降序
                        System.out.println("按照姓名降序");
                        list = workerService.queryWorkerByNameDown(page);
                    }
                }

            } else if (paixu_query_type.equals("paixuSelectByWorkage")) {
                //按照工龄升序
                if(up != null) {
                    if (up.equals("up")) {
                        System.out.println("按照工龄升序");
                        list = workerService.queryWorkerByWorkageUp(page);
                    }
                }
                if(down != null){
                    if(down.equals("down")){
                        //按照工龄降序
                        System.out.println("按照工龄降序");
                        list = workerService.queryWorkerByWorkageDown(page);
                    }
                }
            }
        }





        System.out.println(list);
        System.out.println("countWorkers"+count);
        model.addAttribute("up",up);
        model.addAttribute("paixu_query_type",paixu_query_type);
        model.addAttribute("down",down);
        model.addAttribute("list", list);
        model.addAttribute("pages", pages);
        model.addAttribute("totalpages", totalpages);

        return "yonghu";
    }
}
